//
//  UserHeadView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/27.
//

import UIKit

class UserHeadView: UIView {
    
    var userData: CompleteInfor?{
        didSet{
            if let data = self.userData {
                self.headView.loadImageWithUrl(data.data2)
            }
        }
    }
    
    var userId: String = ""{
        didSet{
            self.getUserDataRequest()
        }
    }
    
    private var headView: UIImageView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.headView = UIImageView()
        let userButton = UIButton().then { make in
            make.addTarget(self, action: #selector(self.headerAction), for: .touchUpInside)
        }
        self.addSubviews([headView,userButton])
        headView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        userButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension UserHeadView{
    private func getUserDataRequest(){
        PanRequestManager<XGL_RequestBaseResponse<UserFollowAndFansNumData>,XGL_CustomRequestError>().request(XGLPersonalRequestAPI(requestAPI: .UserFollowsAndFansNum(userId: self.userId))) { result in
            switch result{
            case .success(let response):
                if let data = response.resultData {
                    if self.userData == nil{
                        self.headView.loadImageWithUrl(data.header)
                    }
                }
                break
            default:
                break
            }
        }
    }
}
extension UserHeadView{
    @objc private func headerAction(){
        guard let window = UIApplication.shared.currentWindow,let controller = window.visibleViewController()  else { return }
        var userId = self.userId
        if let data = self.userData {
            userId = data.userId.description
        }
        if userId != XGL_userDataManager.share.userId{
            let vc = XGL_userDataViewController(userId: userId){
                controller.navigationController?.popViewController()
            }
            controller.navigationController?.pushViewController(vc)
        }else{
//            let vc = XGL_PersonalViewController()
//            window.visibleViewController().navigationController?.pushViewController(vc)
        }
    }
}
